package tool.paoshang;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.FileHeader;
import tool.windows.WindowsTool;

import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class GameClient {

    public static void validate(String msg) {
        Thread t = new Thread(()->{
            for(int i=0;i<60;i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                java.awt.Toolkit.getDefaultToolkit().beep();
            }
        });
        t.start();
        JOptionPane.showMessageDialog(null,"123");
        t.stop();
        WindowsTool.getForce(PaoShang.hwnd);
        Luxian.sleep(500);
    }

    public static File [] unzip(String zip, String passwd) throws ZipException {
        File zipFile = new File(zip);
        File parentDir = new File("");
        return unzip(zipFile, parentDir.getAbsolutePath(), passwd);
    }

    public static File [] unzip(File zipFile, String dest, String passwd) throws ZipException {
        ZipFile zFile = new ZipFile(zipFile);
        zFile.setFileNameCharset("GBK");
        if (!zFile.isValidZipFile()) {
            throw new ZipException("压缩文件不合法,可能被损坏.");
        }
        File destDir = new File(dest);
        if (destDir.isDirectory() && !destDir.exists()) {
            destDir.mkdir();
        }
        if (zFile.isEncrypted()) {
            zFile.setPassword(passwd.toCharArray());
        }
        zFile.extractAll(dest);

        List<FileHeader> headerList = zFile.getFileHeaders();
        List<File> extractedFileList = new ArrayList<File>();
        for(FileHeader fileHeader : headerList) {
            if (!fileHeader.isDirectory()) {
                extractedFileList.add(new File(destDir,fileHeader.getFileName()));
            }
        }
        File [] extractedFiles = new File[extractedFileList.size()];
        extractedFileList.toArray(extractedFiles);
        return extractedFiles;
    }
}
